/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import net.worlds.Main;
import net.worlds.MenuView;
import net.worlds.Util;

/**
 *
 * @author Jacob
 */
public class MenuMain extends EMain{
    private MenuView view;
    private EStickyFrame topMenu;
    private LoginFrame loginFrame;
    private EFrame rightFrame;
    private EButton editorButton = new EButton("Open Editor");
    private EFrame menuFrame;
    private Color gray = new Color(0, 0, 0, 100);
    private int menuY = 5;
    private Font menuFont = new Font("Arial", Font.BOLD, 16);
    
    private ChatPanel chat;
    private ChatUserPanel chatUsers;
    
    private EHTMLFrame gamesPanel;
    private EHTMLFrame myGamesPanel;
    private EHTMLFrame multiplayerPanel;
    
    private EFrame mainPanel;
    private EFrame leftPanel;
    
    public MenuMain(MenuView view, int width, int height)
    {
        super(width, height);
        this.view = view;
        topMenu = new EStickyFrame(this, -1, 24, 0, 0, -1, 0);
        topMenu.setUndecorated(true);
        topMenu.setBackgroundColor(new Color(127, 136, 154));
        
        loginFrame = new LoginFrame(this, 8);
        loginFrame.setVisible(true);
        
        rightFrame = new EStickyFrame(this, 85, 24, 0, loginFrame.getWidth() + 8, -1, 0);
        rightFrame.setBackgroundColor(new Color(0, 0, 0, 0));
        rightFrame.setUndecorated(true);
        rightFrame.setLayer(2);
        editorButton.setHeight(24);
        editorButton.setActionListener(new EActionListener()
                {
                    @Override
                    public void actionPerformed() {
                        try {
                            Main.loadFromResource("/default.worlds", true);
                        } catch (IOException ex) {
                        }
                    }
                });
        rightFrame.addChild(1, editorButton);
        
        ELabel versionLabel = new ELabel("2DWorlds v"+Main.version);
        versionLabel.setHeight(20);
        versionLabel.setX(3);
        topMenu.addChild(1, versionLabel);
        
        chat = new ChatPanel(this);
        chatUsers = new ChatUserPanel();
        gamesPanel = new EHTMLFrame(MenuMain.this, -1, -1, 230, 15, 35, 15, "http://"+Main.siteURL+"/game/web/games.php");
        myGamesPanel = new EHTMLFrame(MenuMain.this, -1, -1, 230, 15, 35, 15, "http://"+Main.siteURL+"/game/web/mygames.php?uid="+Main.getLoginID()+"&key="+Main.getLoginKey());
        multiplayerPanel = new EHTMLFrame(MenuMain.this, -1, -1, 230, 15, 35, 15, "http://"+Main.siteURL+"/game/web/mp.php?uid="+Main.getLoginID()+"&key="+Main.getLoginKey());
        
        
        mainPanel = null;
        leftPanel = null;
        
        menuFrame = new EFrame(this, 10, 35, 200, 200);
        menuFrame.setUndecorated(true);
        menuFrame.setBackgroundColor(shadowColor);
        
        addChild(1, menuFrame);
        addMenuItem("Chat", chat, chatUsers).click();
        addMenuItem("Multiplayer Games", multiplayerPanel, null);
        addMenuItem("Play Games", gamesPanel, null);
        addMenuItem("Make Games", myGamesPanel, null);
    }
    private ELabel addMenuItem(String text, final EFrame view, final EFrame leftView)
    {
        view.setVisible(false);
        ELabel l = new EMenuItem(text);
        l.setHoverColor(new Color(16, 16, 255, 16));
        l.setFont(menuFont);
        l.setY(menuY);
        l.setTextOffset(10);
        l.setHeight(l.getHeight() + 6);
        l.setWidth(menuFrame.getWidth());
        l.setActionListener(new EActionListener()
        {
            @Override
            public void actionPerformed() {
                if(mainPanel == view)
                    return;
                else
                {
                    if(mainPanel != null)
                        mainPanel.setVisible(false);
                    if(leftPanel != null)
                        leftPanel.setVisible(false);

                    mainPanel = view;
                    leftPanel = leftView;
                }
                view.setVisible(true);
                if(leftView != null)
                    leftView.setVisible(true);
            }
        });
        menuY += 32;
        menuFrame.addChild(1, l);
        addChild(1, view);
        if(leftView != null)
        {
            leftView.setVisible(false);
            addChild(1, leftView);
        }
        return l;
    }
    public boolean canChat()
    {
        if(!chat.visible)
            return false;
        else
        {
            Rectangle2D bounds = EDGESettings.defaultFont.getStringBounds(Main.siteChatBarText, Util.fontRendererContext);
            return bounds.getWidth() < 510;
        }
    }
    public void sendChat()
    {
        final String text = Main.siteChatBarText;
        Main.getScriptContext().run(new Runnable()
        {
            public void run() {
                SiteChat.send(text);
            }
        });
    }
}
